#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys
import colorama

# Initialize colorama.
colorama.init()

# File logger.
__filelogger = None

class LoggerLevel(object):
    ERROR = 5
    GOOD = 5
    WARN = 4
    TITLE = 4
    INFO = 3
    VERBOSE = 2

# Logger level.
__log_level = LoggerLevel.INFO

def set_filelogger(name):
    '''
    '''
    global __filelogger

    assert name, 'Argument "name" cannot be null or empty!'
    assert not __filelogger, 'File logger has been assigned!'
    
    __filelogger = open(name, 'wt')

def set_log_level(l):
    '''
    '''
    global __log_level

    __log_level = l

def log_file(msg):
    '''Write message to file logger if exists. 
    '''
    global __filelogger

    if __filelogger:
        print(msg, file=__filelogger)
        __filelogger.flush()

def log_error(msg):
    '''Write ERROR message.
    '''
    global __log_level

    if __log_level > LoggerLevel.ERROR:
        return None

    print(colorama.Style.BRIGHT + colorama.Fore.RED + msg)
    log_file(msg)

def log_warn(msg):
    '''Write WARN message.
    '''
    global __log_level

    if __log_level > LoggerLevel.WARN:
        return None

    print(colorama.Style.BRIGHT + colorama.Fore.YELLOW + msg)
    log_file(msg)

def log_title(msg):
    '''Write TITLE
    '''
    global __log_level

    if __log_level > LoggerLevel.TITLE:
        return None

    print(colorama.Style.BRIGHT + colorama.Fore.WHITE + msg);
    log_file(msg)

def log_info(msg):
    '''Write INFORMATION message.
    '''
    global __log_level

    if __log_level > LoggerLevel.INFO:
        return None

    print(colorama.Style.DIM + colorama.Fore.WHITE + msg)
    log_file(msg)

def log_verbose(msg):
    '''Write VERBOSE message.
    '''
    global __log_level

    if __log_level > LoggerLevel.VERBOSE:
        return None

    print(colorama.Style.BRIGHT + colorama.Fore.BLACK + msg)
    log_file(msg)

def log_good(msg):
    '''Write GOOD message.
    '''
    global __log_level

    if __log_level > LoggerLevel.GOOD:
        return None

    print(colorama.Style.BRIGHT + colorama.Fore.GREEN + msg)
    log_file(msg)

def close_log():
    '''Shutdown console logger.
    '''
    global __filelogger

    print(colorama.Style.RESET_ALL + colorama.Fore.RESET + colorama.Back.RESET)

    if __filelogger:
        __filelogger.close()

if __name__ == '__main__':
    # Test codes here.

    set_filelogger('a.txt')
    set_log_level(LoggerLevel.VERBOSE)

    log_error('Error')
    log_warn('Warning')
    log_title('Title')
    log_info('Information')
    log_verbose('Verbose')
    log_good('Good')

    close_log()
